<template>
  <div id="app">

    <div class="todo-container">
      <div class="todo-wrap">
        <my-header></my-header>
        <my-list :todo-list="todoList"></my-list>
        <my-footer :todo-list="todoList"></my-footer>
      </div>
    </div>


  </div>
</template>

<script>

import MyHeader from "@/components/MyHeader";
import MyFooter from "@/components/MyFooter";
import MyList from "@/components/MyList";

export default {
  name: 'App',
  data() {
    return {
      todoList: JSON.parse(localStorage.getItem("todoList")) || []
    }
  },
  components: {
    MyFooter,
    MyList,
    MyHeader
  },
  mounted() {
    this.$bus.$on("changeStatus", (id) => {
      this.todoList.forEach((e) => {
        if (e.id === id) {
          e.isDone = !e.isDone;
        }
      })
    });
    this.$bus.$on("delTodo", (id) => {
      this.todoList = this.todoList.filter((e) => e.id !== id
      )
    });

    this.$bus.$on("addTodo", (obj) => {
      this.todoList.push(obj)
    });

    this.$bus.$on("clearDone",()=>{
      this.todoList= this.todoList.filter(e=>!e.isDone)
    })

    this.$bus.$on("changeName",(id,name)=>{
      this.todoList.forEach((e) => {
        if (e.id === id) {
          e.name = name;
        }
      })

    })

  },
  watch: {
    todoList: {
      handler(val) {
        localStorage.setItem("todoList", JSON.stringify(val));
      },
      deep: true
    }
  }
}
</script>

<style>
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-edit {
  color: #fff;
  background-color: #f4dad9;
  border: 1px solid #b53b34;
}



.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
